package com.apestech.framework.run.modul;

import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.run.datamapper.SingleDataMappingModule;

import java.util.ArrayList;
import java.util.Vector;

public class ExecSingleParam {
    private String className;
    private String methodName;
    private String input;
    private String output;
    private JSONObject in;
    private JSONObject out;
    private ArrayList<SingleDataMappingModule> inlist;
    private ArrayList<SingleDataMappingModule> outlist;

    private String queryId;  //查询ID
    private String http;  //http地址
    private String conditionId;  //条件类型ID
    private String pricingProcessId;  //定价过程ID
    private String ruleEngineId;

    private String type;

    private void addParam(Object input, Object output){
        if(input==null){
            this.input="";
            this.in=null;
        } else {
            if(input instanceof String){
                this.input= (String) input;
            } else {
                if(input instanceof ArrayList){
                    this.inlist= (ArrayList<SingleDataMappingModule>) input;
                } else {
                    this.in= (JSONObject) input;
                }
            }
        }

        if(output==null){
            this.output="";
            this.out=null;
        } else {
            if(output instanceof String){
                this.output= (String) output;
            } else {
                if(output instanceof ArrayList){
                    this.outlist= (ArrayList<SingleDataMappingModule>) output;
                } else {
                    this.out= (JSONObject) output;
                }
            }
        }
    }

    public void addMethod(String className, String methodName, Object input, Object output){
        this.className=className;
        this.methodName=methodName;
        addParam(input, output);
        this.type="method";
    }

    public void addQuery(String queryId, Object input, Object output){
        this.queryId=queryId;
        addParam(input, output);
        this.type="query";
    }

    public void addPricingProcess(String pricingProcessId, Object input, Object output){
        this.pricingProcessId=pricingProcessId;
        addParam(input, output);
        this.type="pricingProcess";
    }

    public void addRuleEngine(String ruleEngineId, Object input, Object output){
        this.ruleEngineId=ruleEngineId;
        addParam(input, output);
        this.type="ruleEngine";
    }

    public void addCondition(String conditionId, Object input, Object output){
        this.conditionId=conditionId;
        addParam(input, output);
        this.type="condition";
    }

    public void addHttp(String http, Object input, Object output){
        this.http=http;
        addParam(input, output);
        this.type="http";
    }

    public String getClassName() {
        return className;
    }

    public String getMethodName() {
        return methodName;
    }

    public String getQueryId() {
        return queryId;
    }

    public String getHttp() {
        return http;
    }

    public String getConditionId() {
        return conditionId;
    }

    public String getPricingProcessId() {
        return pricingProcessId;
    }

    public String getRuleEngineId() {
        return ruleEngineId;
    }

    public String getType() {
        return type;
    }

    public Object getInput() {
        if(this.in!=null){
            return in;
        } else if(this.inlist!=null){
            return inlist;
        }
        return input;
    }

    public boolean hasInput(){
        if(this.in==null && this.inlist==null && "".equals(this.input)){
            return false;
        }
        return true;
    }

    public boolean hasOutput(){
        if(this.out==null && this.outlist==null && "".equals(this.output)){
            return false;
        }
        return true;
    }

    public Object getOutput() {
        if(this.out!=null){
            return out;
        } else if(this.outlist!=null){
            return outlist;
        }
        return output;
    }

    public String getInputStr() throws Exception {
        Object in1=getInput();
        if(in1==null){
            return "null";
        } else {
            if(in1 instanceof String){
                return "\""+in1.toString()+"\"";
            } else if(in1 instanceof ArrayList) {
                return in1.toString();
            } else{
                return JSON.toJSONString(in1);
            }
        }
    }

    public String getOutputStr() throws Exception {
        Object out1=getOutput();
        if(out1==null){
            return "null";
        } else {
            if(out1 instanceof String){
                return "\""+out1.toString()+"\"";
            } else if(out1 instanceof ArrayList) {
                return out1.toString();
            } else {
                return JSON.toJSONString(out1);
            }
        }
    }

    public String toString(){
        try {
            switch(this.type){
                case "method":
                    return "{\"className\":\""+className+"\", \"methodName\":\""+methodName+"\", \"input\":"+getInputStr()+", \"output\":"+getOutputStr()+"}";
                case "query":
                    return "{\"query id\":\""+queryId+"\", \"input\":"+getInputStr()+", \"output\":"+getOutputStr()+"}";
                case "pricingProcess":
                    return "{\"pricing process id\":\""+pricingProcessId+"\", \"input\":"+getInputStr()+", \"output\":"+getOutputStr()+"}";
                case "ruleEngine":
                    return "{\"rule engine id\":\""+ruleEngineId+"\", \"input\":"+getInputStr()+", \"output\":"+getOutputStr()+"}";
                case "condition":
                    return "{\"condition id\":\""+conditionId+"\", \"input\":"+getInputStr()+", \"output\":"+getOutputStr()+"}";
                case "http":
                    return "{\"http\":\""+http+"\", \"input\":"+getInputStr()+", \"output\":"+getOutputStr()+"}";
                default:
                    return "";
            }
        } catch (Exception e) {
            return "";
        }
    }

}
